using System;
using System.Collections;
using System.Drawing;
using System.Globalization;
using PickGold.Charting;

namespace PickGold.Charting.ChartTypes
{
	internal class ThreeLineBreakChart : IChartType, IDisposable
	{
		public void AddSmartLabelMarkerPositions(CommonElements common, ChartArea area, Series series, ArrayList list)
		{
		}

		public void Dispose()
		{
			this.Dispose(true);
			GC.SuppressFinalize(this);
		}

		protected virtual void Dispose(bool disposing)
		{
		}

		private static void FillThreeLineBreakData(Series series, Series originalData)
		{
			int index = 0;
			if (series.IsCustomPropertySet("UsedYValue"))
			{
				try
				{
					index = int.Parse(series["UsedYValue"], CultureInfo.InvariantCulture);
				}
				catch
				{
					throw new InvalidOperationException(SR.ExceptionThreeLineBreakUsedYValueInvalid);
				}
				if (index >= series.YValuesPerPoint)
				{
					throw new InvalidOperationException(SR.ExceptionThreeLineBreakUsedYValueOutOfRange);
				}
			}
			int num2 = 3;
			if (series.IsCustomPropertySet("NumberOfLinesInBreak"))
			{
				try
				{
					num2 = int.Parse(series["NumberOfLinesInBreak"], CultureInfo.InvariantCulture);
				}
				catch
				{
					throw new InvalidOperationException(SR.ExceptionThreeLineBreakNumberOfLinesInBreakFormatInvalid);
				}
				if (num2 <= 0)
				{
					throw new InvalidOperationException(SR.ExceptionThreeLineBreakNumberOfLinesInBreakValueInvalid);
				}
			}
			ArrayList list = new ArrayList();
			double naN = double.NaN;
			double d = double.NaN;
			int num5 = 0;
			int num6 = 0;
			int num7 = 0;
			foreach (DataPoint point in originalData.Points)
			{
				int num8 = 0;
				if (point.IsEmpty)
				{
					num7++;
					continue;
				}
				if (double.IsNaN(naN) || double.IsNaN(d))
				{
					d = point.YValues[index];
					naN = point.YValues[index];
					num7++;
					continue;
				}
				Color transparent = Color.Transparent;
				string text = point["PriceUpColor"];
				if (text == null)
				{
					text = series["PriceUpColor"];
				}
				if (text != null)
				{
					try
					{
						ColorConverter converter = new ColorConverter();
						transparent = (Color)converter.ConvertFromString(null, CultureInfo.InvariantCulture, text);
					}
					catch
					{
						throw new InvalidOperationException(SR.ExceptionThreeLineBreakUpBrickColorInvalid);
					}
				}
				if (point.YValues[index] > d)
				{
					num8 = 1;
				}
				else if (point.YValues[index] < naN)
				{
					num8 = -1;
				}
				else
				{
					num8 = 0;
				}
				if (num8 != 0)
				{
					if (num6 == num8)
					{
						num5++;
					}
					else
					{
						if (num5 >= num2)
						{
							switch (num8)
							{
								case 1:
									{
										double minValue = double.MinValue;
										for (int i = 0; i < list.Count; i += 2)
										{
											if (((double)list[i]) > minValue)
											{
												minValue = (double)list[i];
											}
										}
										if (point.YValues[index] <= minValue)
										{
											num8 = 0;
										}
										break;
									}
								case -1:
									{
										double maxValue = double.MaxValue;
										for (int j = 1; j < list.Count; j += 2)
										{
											if (((double)list[j]) < maxValue)
											{
												maxValue = (double)list[j];
											}
										}
										if (point.YValues[index] >= maxValue)
										{
											num8 = 0;
										}
										break;
									}
							}
						}
						if (num8 != 0)
						{
							num5 = 1;
						}
					}
					if (num8 != 0)
					{
						DataPoint item = point.Clone();
						item["OriginalPointIndex"] = num7.ToString(CultureInfo.InvariantCulture);
						item.series = series;
						item.YValues = new double[2];
						item.XValue = point.XValue;
						item.Tag = point;
						if (num8 == 1)
						{
							item.YValues[1] = d;
							item.YValues[0] = point.YValues[index];
							naN = d;
							d = point.YValues[index];
							item.Color = transparent;
							if (item.BorderWidth < 1)
							{
								item.BorderWidth = 1;
							}
							if (item.BorderDashStyle == ChartDashStyle.NotSet)
							{
								item.BorderDashStyle = ChartDashStyle.Solid;
							}
							if (((item.BorderColor == Color.Empty) || (item.BorderColor == Color.Transparent)) && ((item.Color == Color.Empty) || (item.Color == Color.Transparent)))
							{
								item.BorderColor = series.Color;
							}
						}
						else
						{
							item.YValues[1] = naN;
							item.YValues[0] = point.YValues[index];
							d = naN;
							naN = point.YValues[index];
						}
						series.Points.Add(item);
						list.Add(d);
						list.Add(naN);
						if (list.Count > (num2 * 2))
						{
							list.RemoveAt(0);
							list.RemoveAt(0);
						}
					}
				}
				if (num8 != 0)
				{
					num6 = num8;
				}
				num7++;
			}
		}

		public virtual Image GetImage(ChartTypeRegistry registry)
		{
			return (Image)registry.ResourceManager.GetObject(this.Name + "ChartType");
		}

		public virtual LegendImageStyle GetLegendImageStyle(Series series)
		{
			return LegendImageStyle.Rectangle;
		}

		public virtual double GetYValue(CommonElements common, ChartArea area, Series series, DataPoint point, int pointIndex, int yValueIndex)
		{
			return point.YValues[yValueIndex];
		}

		public virtual void Paint(ChartGraphics graph, CommonElements common, ChartArea area, Series seriesToDraw)
		{
		}

		internal static void PrepareData(Series series)
		{
			if ((string.Compare(series.ChartTypeName, "ThreeLineBreak", StringComparison.OrdinalIgnoreCase) == 0) && series.IsVisible())
			{
				Chart chart = series.Chart;
				if (chart == null)
				{
					throw new InvalidOperationException(SR.ExceptionThreeLineBreakNullReference);
				}
				ChartArea area = chart.ChartAreas[series.ChartArea];
				foreach (Series series2 in chart.Series)
				{
					if ((series2.IsVisible() && (series2 != series)) && (area == chart.ChartAreas[series2.ChartArea]))
					{
						throw new InvalidOperationException(SR.ExceptionThreeLineBreakCanNotCobine);
					}
				}
				Series item = new Series("THREELINEBREAK_ORIGINAL_DATA_" + series.Name, series.YValuesPerPoint);
				item.Enabled = false;
				item.IsVisibleInLegend = false;
				chart.Series.Add(item);
				foreach (DataPoint point in series.Points)
				{
					item.Points.Add(point);
				}
				series.Points.Clear();
				if (series.IsCustomPropertySet("TempDesignData"))
				{
					item["TempDesignData"] = "true";
				}
				series["OldXValueIndexed"] = series.IsXValueIndexed.ToString(CultureInfo.InvariantCulture);
				series["OldYValuesPerPoint"] = series.YValuesPerPoint.ToString(CultureInfo.InvariantCulture);
				series.ChartType = SeriesChartType.RangeColumn;
				series.IsXValueIndexed = true;
				series.YValuesPerPoint = 2;
				if ((series.ChartArea.Length > 0) && series.IsXValueDateTime())
				{
					Axis axis = area.GetAxis(AxisName.X, series.XAxisType, series.XSubAxisName);
					if ((axis.Interval == 0.0) && (axis.IntervalType == DateTimeIntervalType.Auto))
					{
						bool flag = false;
						double maxValue = double.MaxValue;
						double minValue = double.MinValue;
						foreach (DataPoint point2 in item.Points)
						{
							if (!point2.IsEmpty)
							{
								if (point2.XValue != 0.0)
								{
									flag = true;
								}
								if (point2.XValue > minValue)
								{
									minValue = point2.XValue;
								}
								if (point2.XValue < maxValue)
								{
									maxValue = point2.XValue;
								}
							}
						}
						if (flag)
						{
							series["OldAutomaticXAxisInterval"] = "true";
							DateTimeIntervalType auto = DateTimeIntervalType.Auto;
							axis.interval = axis.CalcInterval(maxValue, minValue, true, out auto, series.XValueType);
							axis.intervalType = auto;
						}
					}
				}
				FillThreeLineBreakData(series, item);
			}
		}

		internal static bool UnPrepareData(Series series)
		{
			bool flag;
			int num;
			if (!series.Name.StartsWith("THREELINEBREAK_ORIGINAL_DATA_", StringComparison.Ordinal))
			{
				return false;
			}
			Chart chart = series.Chart;
			if (chart == null)
			{
				throw new InvalidOperationException(SR.ExceptionThreeLineBreakNullReference);
			}
			Series fromSeries = chart.Series[series.Name.Substring(0x1d)];
			Series.MovePositionMarkers(fromSeries, series);
			fromSeries.Points.Clear();
			if (!series.IsCustomPropertySet("TempDesignData"))
			{
				foreach (DataPoint point in series.Points)
				{
					fromSeries.Points.Add(point);
				}
			}
			fromSeries.ChartType = SeriesChartType.ThreeLineBreak;
			bool flag2 = bool.TryParse(fromSeries["OldXValueIndexed"], out flag);
			fromSeries.IsXValueIndexed = flag2 && flag;
			if (int.TryParse(fromSeries["OldYValuesPerPoint"], NumberStyles.Any, CultureInfo.InvariantCulture, out num))
			{
				fromSeries.YValuesPerPoint = num;
			}
			fromSeries.DeleteCustomProperty("OldXValueIndexed");
			fromSeries.DeleteCustomProperty("OldYValuesPerPoint");
			series["OldAutomaticXAxisInterval"] = "true";
			if (fromSeries.IsCustomPropertySet("OldAutomaticXAxisInterval"))
			{
				fromSeries.DeleteCustomProperty("OldAutomaticXAxisInterval");
				if (fromSeries.ChartArea.Length > 0)
				{
					Axis axis = chart.ChartAreas[fromSeries.ChartArea].GetAxis(AxisName.X, fromSeries.XAxisType, fromSeries.XSubAxisName);
					axis.interval = 0.0;
					axis.intervalType = DateTimeIntervalType.Auto;
				}
			}
			chart.Series.Remove(series);
			return true;
		}

		public virtual bool ApplyPaletteColorsToPoints
		{
			get
			{
				return false;
			}
		}

		public bool CircularChartArea
		{
			get
			{
				return false;
			}
		}

		public virtual bool DataPointsInLegend
		{
			get
			{
				return false;
			}
		}

		public virtual bool ExtraYValuesConnectedToYAxis
		{
			get
			{
				return true;
			}
		}

		public virtual bool HundredPercent
		{
			get
			{
				return false;
			}
		}

		public virtual bool HundredPercentSupportNegative
		{
			get
			{
				return false;
			}
		}

		public virtual string Name
		{
			get
			{
				return "ThreeLineBreak";
			}
		}

		public virtual bool RequireAxes
		{
			get
			{
				return true;
			}
		}

		public bool SecondYScale
		{
			get
			{
				return false;
			}
		}

		public bool SideBySideSeries
		{
			get
			{
				return false;
			}
		}

		public virtual bool Stacked
		{
			get
			{
				return false;
			}
		}

		public bool StackSign
		{
			get
			{
				return false;
			}
		}

		public virtual bool SupportLogarithmicAxes
		{
			get
			{
				return true;
			}
		}

		public virtual bool SupportStackedGroups
		{
			get
			{
				return false;
			}
		}

		public virtual bool SwitchValueAxes
		{
			get
			{
				return false;
			}
		}

		public virtual int YValuesPerPoint
		{
			get
			{
				return 1;
			}
		}

		public virtual bool ZeroCrossing
		{
			get
			{
				return false;
			}
		}
	}
}

